package com.base.themvx.utils

import android.content.SharedPreferences
import android.os.Parcelable
import com.base.themvx.defNonSyncLazy
import com.tencent.mmkv.MMKV

/**
 ******************(^_^)***********************<br>
 * Author: fee(QQ/WeiXin:1176610771)<br>
 * <P>DESC:
 * 项目中统一使用的本地持久化 访问者
 * </p>
 * ******************(^_^)***********************
 */
class SpVisitor private constructor(sp: SharedPreferences) : SharedPreferencesVistor(sp) {

    companion object Builder {
        private val mVisitor by defNonSyncLazy {
            SpVisitor(MMKV.defaultMMKV())
        }

        /**
         * 当前登录的用户 id
         */
        private val mCurUserId: String
            get() {//to do: 这个　userId有时拿不到
                if (!curUserId.isNullOrBlank()){
                    return curUserId!!
                }
                return ""
            }

        var curUserId: String? = ""

        @JvmStatic
        fun putV(key: String, defValue: Any): Boolean = mVisitor.putValue(key, defValue)

        /**
         * 缓存和当前登录用户相关的数据
         */
        @JvmStatic
        fun putVOfUser(key: String, defValue: Any): Boolean = mVisitor.putValue("${mCurUserId}_$key", defValue)

        @JvmStatic
        fun <T> getV(key: String, defValue: Any): T = mVisitor.getValue(key, defValue) as T

        /**
         * 获取和当前登录用户相关的数据
         */
        @JvmStatic
        fun <T> getVOfUser(key: String, defValue: Any): T = mVisitor.getValue("${mCurUserId}_$key", defValue) as T

        @JvmStatic
        fun getMmkv() = mVisitor.asMmkv()
    }

    private fun test(defValue: Any) {
        when(defValue){
            is String -> {

            }
            is Long -> {
            }

            is Int -> {
            }

            is Boolean -> {
            }

            is Float -> {
            }

            is Double -> {

            }

            is Parcelable -> {
                //to do : 这里统一不了
            }

            is MutableSet<*> -> {

            }

            is ByteArray -> {

            }
        }
    }

    override fun asMmkv(): MMKV{
        return super.asMmkv()!!
    }
}